const global = getApp().globalData;
const wxApi = global.wxApi;
const Api = global.Api;
const store = global.store;
const subjectsBaseUrl = "https://ia.jydsapp.com/dfc/ns/nine-subjects";
const subjectsMap = {
  1: "", // 英语
  2: "/gzyw.html", // 语文
  3: "/czsx.html", // 数学
  4: "/czwl.html", // 物理
  5: "/gzhx.html", // 化学
  6: "/gzsw.html", // 生物
  7: "/czls.html", // 历史
  8: "/czdl.html", // 地理
  9: "/czzz.html", // 政治
}
Component({
  data: {
    promptShow: false,
    subjectsData: []
  },
  properties: {
    subjects: {
      type: Array,
      value: [],
      observer: '__subjectsObserver'
    },
    accounts: {
      type: null,
      observer: '__accountsObserver'
    }
  },
  attached() {
  },
  methods: {
    __accountsObserver(val) {
      this.parseSubjects(this.data.subjects);
    },
    __subjectsObserver(arr) {
      this.parseSubjects(arr);
    },
    parseSubjects(subjectsArr) {
      let subjects = [];
      let accounts = this.data.accounts || {}
      for(let i = 0, len = subjectsArr.length; i < len; i++) {
        let subject = subjectsArr[i];
        let obj = accounts[subject.id] || {
          no: 0,
          time: ""
        };
        subject.showBooksCount = (obj.no < subject.clickTimesThreshold || subject.publishTime != obj.time) && subject.newTextbooksCount ? true : false
        subjects.push(subject);
      }
      console.log('解析后九大学科')
      console.log(subjects);
      this.setData({
        subjectsData: subjects
      })
    },
    // __accountsObserver(val) {
    //   console.log('accounts change.')
    //   console.log(val);
    //   this.__accountsObserver(this.data.subjectsData);
    // },
    onSure() {
      console.log( 'onsure' );
      Api.fireSubject( this.subjectId, (res, code) => {
      } )
    },
    onSubjectTap( e ) {
      
      const dataset = e.currentTarget.dataset;
      let subject = dataset.subject;
      subject.linkUrl = subjectsBaseUrl + subjectsMap[subject.id];
      this.triggerEvent('click', subject)
    }
  }
})